from django.http import HttpResponse,JsonResponse
from django.shortcuts import render
from django.views.decorators.csrf import csrf_exempt
from content.form import SearchForm
from content.models import Goods, UserRating, UserShopping
from login.models import User
from common.collaborative_filtering import calculate_daily_recommendations
from common.collaborative_filtering_1 import recommend_goods
from common.add_data import test
from common import recommand
import json
search_form = SearchForm()
top_5 = Goods.objects.order_by('-rating').all()[0:5]


from common.recommand import Recommender

recommender = Recommender()
recommender.load_data()

@csrf_exempt
def index(request):

    user = User.objects.get(id=request.session.get('user_id'))
    # 跑一下推荐算法，展示的时候用
    calculate_daily_recommendations()
    recommend_goods(user)
    recommend_data = Goods.objects.filter(is_recommended=True).all()[0:5]

    # user_id = request.session.get('user_id')
    # user1_recommends = recommender.recommend(user_id=user_id)
    # print("推荐商品ID：", user1_recommends)

    if not recommend_data:
        recommend_data = Goods.objects.all().order_by('-rating')[0:5]
    return render(request, 'content/index.html',
                  {'search_form': search_form, 'top_5': top_5, 'recommend_data': recommend_data})

@csrf_exempt
def add_data(request):
    test()
    recommend_data = Goods.objects.filter(is_recommended=True).all()[0:5]
    return render(request, 'content/index.html',
                  {'search_form': search_form, 'top_5': top_5, 'recommend_data': recommend_data})


@csrf_exempt
def add_shopping(request):
    user = User.objects.get(id=request.session.get('user_id'))
    good = Goods.objects.get(id=request.GET.get('id'))
    if UserShopping.objects.filter(user=user, goods=good).exists():
        return HttpResponse('201')
    UserShopping.objects.create(user=user, goods=good)
    return HttpResponse('200')


@csrf_exempt
def goods(request):
    search_key = request.GET.get('name', '')
    print('====', request.GET.get('name'))
    search_data = Goods.objects.filter(name__contains=search_key).all()
    return render(request, 'content/goods.html',
                  {'search_form': search_form, 'top_5': top_5, 'search_data': search_data})


@csrf_exempt
def good(request):
    if request.method == 'GET':
        id = request.GET.get('id')
        good = Goods.objects.get(id=id)
        # 相似商品,排除good本身
        similar_goods = Goods.objects.filter(category=good.category).exclude(id=id).all()
        # 如果大于3个,就取三个，不够3个就全部
        if len(similar_goods) > 3:
            similar_goods = similar_goods[0:3]
        return render(request, 'content/good.html',
                      {'search_form': search_form, 'top_5': top_5, 'good': good, 'similar_goods': similar_goods})

    data = json.loads(request.body.decode('utf-8'))
    # 添加一条评分数据到UserRating中

    rating = float(data['rating'])
    user = User.objects.get(id=request.session.get('user_id'))
    good = Goods.objects.get(id=data['good_id'])
    if UserRating.objects.filter(user=user, goods=good).exists():
        return HttpResponse('201')
    UserRating.objects.create(rating=rating, user=user, goods=good)
    # 同时将商品的官方评分改为自己的评分
    good.rating = rating
    good.save()

    return HttpResponse('200')
